mui.init();
var room_id,game_id,room_name,username,game_OverTime=180,timeItem=null;
var _opt = getToKen();
var curWebview;
var showRedPacket = function(obj){
	var mainBody = document.getElementById("redpacketbody");
	mainBody.innerHTML = "";
	template.defaults.imports.currencyName = function(_currencyName){
		return _currencyName;
	};
	obj['datas']['currencyName'] = currencyName;
	var html = template('redTemplate', obj); 
	mainBody.innerHTML = html;
	document.getElementById("betCoinNumberKeyBoard").innerHTML = template('betCoinNumberTemplat', obj); 
	
}
var red5 = {
	onopen : function(){
		_opt["type"] = "joingame";
		_opt['game_id'] = game_id;
		_opt['room_id'] = room_id;
		isDebug && console.log(JSON.stringify(_opt));
		ws.send(JSON.stringify(_opt));
	},
	onmessage : function(e){
		var data = eval("("+e.data+")");
		isDebug && console.log(JSON.stringify(data));
	    switch(data['type']){
	    	case 'ping':
	            ws.send('{"type":"pong"}');
	            break;
	        case 'pong':
	        	//通知更新人数
	        	fireData = {
	        		type : 'online',
		        	online : data['online'],
		        };
	            pushSubWebview(fireData);
	        	break;
	    	case 'joingame'://列出可用的红包
	    		if(data['state']!=200){
					openLoginWindow(room_id,curWebview);
					return false;
				}
				showRedPacket(data);
	    		break;
	    	case 'gamerule'://规则
	    		document.getElementById("game-guide").innerHTML = data['rule'];
	    		break;
	    	case 'sendred'://发红包后收到的信息
	    		if(data['state']==200){
	    			//显示红包状态
	    			document.getElementById("send-red-packet").style.display = "block";
					document.getElementById("send-red-packet-body").style.display = "block";
					document.getElementById("redpacket-grab-body").style.display = "block";
					data['datas']['room_name'] = room_name;
					html = template('sendIssueInfoTemplate', data);
	    			document.getElementById("send-issue-info").innerHTML = html;
	    			//红包倒计时
	    			game_OverTime = data['datas'][0]['overtime'];
	    			isDebug && console.log("game_OverTime="+game_OverTime);
	    			timeItem = game_setTime(document.getElementById("grabCountdown"));
	    		}
	    		break;
	    	case 'newred': //有用户发了新的红包
	    		var html = template('redTemplate', data);
	    		document.getElementById("redpacketbody").innerHTML += html;
	    		break;
	    	case 'grab'://抢红包
	    		if(data['state']==200){
	    			//显示红包状态
	    			document.getElementById("send-red-packet").style.display = "block";
					document.getElementById("send-red-packet-body").style.display = "block";
					document.getElementById("redpacket-grab-body").style.display = "block";
					
	    		}
	    		break;
	    }
	}
};
window.onload = function(){  
  //从服务器获取数据  
  //业务数据获取完毕，并已插入当前页面DOM；  
  //注意：若为ajax请求，则需将如下代码放在处理完ajax响应数据之后；  
  mui.plusReady(function(event){
  	curWebview = plus.webview.currentWebview();
  	room_id = curWebview.room_id;
  	game_id = curWebview.game_id;
  	room_name = curWebview.room_name;
  	username = curWebview.username;
	document.querySelector(".mui-title").innerText = room_name;
	//拉取数据
	room_connect(red5.onopen,red5.onmessage,true);
    //关闭等待框  
    plus.nativeUI.closeWaiting();  
    //显示当前页面  
    mui.currentWebview.show();
    //阻尼系数
	var deceleration = mui.os.ios?0.003:0.0009;
	mui('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: true, //是否显示滚动条
		deceleration:deceleration
	});
	//绑定事件
	bindtap();
  });  
}

//事件处理
var bindtap = function(){
	//重新登录后重新取数据
	window.addEventListener("loginSuccess",function(e){
		isDebug && console.log("收到登录成功："+JSON.stringify(e));
		_opt["type"] = "joingame";
		_opt['game_id'] = game_id;
		_opt['room_id'] = room_id;
		isDebug && console.log(JSON.stringify(_opt));
		ws.send(JSON.stringify(_opt));
	},false);
	//抢红包
	mui("#redpacketbody").off("tap","a");
	mui("#redpacketbody").on("tap","a",function(e){
		red_id = this.getAttribute("red_id"); 
		//如果人数已达到
		this.parentNode.parentNode.removeChild(this.parentNode);
	});
	//导航点击事件
	mui("nav").off("tap","a");
	mui("nav").on("tap","a",function(e){
		var _href = this.getAttribute("href");
		var _opt;
		document.getElementById("send-red-packet").style.display = "block";
		document.getElementById("send-red-packet-body").style.display = "block";
		switch(_href){
			case "send-redpacket-btn":
				document.getElementById("betCoinNumberKeyBoard").style.display = "block";
				break;
			case "redpacket-guide":
				_opt = {
					"type" : "gamerule",
					"game_id" : game_id,
				}
				document.getElementById("redpacket-guide").style.display = "block";
				ws.send(JSON.stringify(_opt));
				break;
			case "redpacket-log":
				_opt = getToKen();
				document.getElementById("redpacket-log").style.display = "block";
				break;
		}
		document.querySelector(".mui-tab-item").classList.remove("mui-active");
	});
	mui("#betCoinNumberKeyBoard").off("tap","a");
	mui("#betCoinNumberKeyBoard").on("tap","a",function(e){
		var _href = this.getAttribute("href");
		switch(_href){
			case "close-betCoinNumberKeyBoard":
				document.getElementById("send-red-packet").style.display = "none";
				document.getElementById("send-red-packet-body").style.display = "none";
				document.getElementById("betCoinNumberKeyBoard").style.display = "none";
				break;
			case "submit":
				var _opt = getToKen();
				var _coin = document.getElementById("okey_coin").innerText;
				_opt['type'] = 'sendred';
				_opt['game_id'] = game_id;
				_opt['coin'] = parseFloat(_coin);
				_opt['room_id'] = room_id;
				isDebug && console.log(JSON.stringify(_opt));
				ws.send(JSON.stringify(_opt));
				break;
			default:
				this.classList.add("mui-active");
				var _usercoin = parseFloat(document.getElementById("usercoin").innerText);
				var _sendcoin = parseFloat(_href);
				if(_usercoin < _sendcoin){
					mui.alert(info[10005]);
					return false;
				}
				document.getElementById("okey_coin").innerText = _href;
				break;
		}
	});
	mui("#redpacket-guide").off("tap","a.mui-icon-left-nav");
	mui("#redpacket-guide").on("tap","a.mui-icon-left-nav",function(e){
		document.getElementById("send-red-packet").style.display = "none";
		document.getElementById("send-red-packet-body").style.display = "none";
		document.getElementById("redpacket-guide").style.display = "none";
	});
	mui("#redpacket-log").off("tap","a.mui-icon-left-nav");
	mui("#redpacket-log").on("tap","a.mui-icon-left-nav",function(e){
		document.getElementById("send-red-packet").style.display = "none";
		document.getElementById("send-red-packet-body").style.display = "none";
		document.getElementById("redpacket-log").style.display = "none";
	});
}
